package com.whl.org.service.impl;

import com.whl.basic.service.impl.BaseServiceImpl;

import com.whl.org.domain.Department;
import com.whl.org.mapper.DepartmentMapper;

import com.whl.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门业务实现类
 */
@Service

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void add(Department department) {
        // 添加之后会返回一个自增长id
        departmentMapper.save(department);
        // 添加的时候对dirPath做处理
        String dirPath = "";
        if(department.getParent_id() == null){ // 没有选上级部门
            dirPath = "/" + department.getId();
        }else { // 选了上级部门或选了上级部门又叉掉了
            // 获取上级部门id
            Long Parent_id = department.getParent_id();
            // 根据上级部门id获取上级部门的dirPath
            String parentDirPath = departmentMapper.findOne(Parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        // 修改的时候对dirPath做处理
        String dirPath = "";
        if(department.getParent_id() == null){ // 没有选上级部门
            dirPath = "/" + department.getId();
        }else { // 选了上级部门或选了上级部门又叉掉了
            // 获取上级部门id
            Long Parent_id = department.getParent_id();
            // 获取上级部门的dirPath
            String parentDirPath = departmentMapper.findOne(Parent_id).getDirPath();
            dirPath = parentDirPath + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }



    /**
     *  业务：查询部门树
     * @return
     */
    @Override
    public List<Department> deptTree() {
        // 1. 一次性查询出所有数据，放到内存中，可以使用map
        // 2. 从所有数据中找出第一层级/顶级部门：这就是我们需要返回的数据
        // 3. 遍历所有部门只要不是第一层级的，找自己的上级，并将自己放入上级的children中

        // 查询所有的部门
        List<Department> all = departmentMapper.findAll();
        // 声明一个map集合
        Map<Long,Department> map = new HashMap<>();
        // 遍历
        for (Department department : all) {
            // 将老部门放入map中
            map.put(department.getId(), department);
        }

        // 前端需要得到的集合数据 - 部门树
        List<Department> deptTree = new ArrayList<>();
        // 遍历
        for (Department department : all) {
           if(department.getParent_id() == null){ // 顶级部门
               // 封装到部门树
               deptTree.add(department);
           }else { // 不是顶级部门
               // 获取父部门的id
               Long parent_id = department.getParent_id();
               // 根据父id从map中去获取父部门 - 以前装进去的对象还是会存在
               Department parentDept = map.get(parent_id);
               // 将自己装到父部门的children中
               parentDept.getChildren().add(department);
           }
        }
        // 返回部门树
        return deptTree;
    }
}
